package Lee_code;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author blh
 * @date 2024年10月24日 14:41
 */
public class Lee_637_1024 {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> res = new ArrayList<>();
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            int totalSize = deque.size();
            double sum = 0;
            for (int i = 0; i < totalSize; i++) {
                TreeNode tempNode = deque.poll();
                sum += tempNode.val;
                if (tempNode.right != null) {
                    deque.offer(tempNode.right);
                }
                if (tempNode.left != null) {
                    deque.offer(tempNode.left);
                }
            }
            double avg = sum / totalSize;
            res.add(avg);
        }
        return res;
    }
}
